package test

import grails.validation.ValidationException;


class UserController {

	UserService userService
	
	static scaffold = true
	
    def index = { redirect(action:"list")}
	
	def menu = {
		
	}
	
	def delete = {
		userService.deleteAllInactiveUsersAndActiveWithLoginZupa()
		render(view: 'menu', model:[info:'pomyslnie usunieto userow'])
	}
	
	def addUser = {
		
	}
	
	def saveUser = {
		println params
		
		User user = new User()
		user.login = params.login
		user.password = params.pass.encodeAsMD5()
		user.email = params.email
		user.birthDate = new Date()
	
		try {
			user.save(failOnError:true)
			return [createdUser: user, dodatkoweDane: 'cokolwiek']
		} catch (ValidationException e) {
			String errorCode = ""
			user.errors.fieldErrors.each {
				errorCode += it.field + "." + it.code + "\n"
			} 
			return [error: errorCode]
		}
	}
}
